package cn.enjoyedu.fksum.service;


import cn.enjoyedu.fksum.dao.DbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SingleThread {

    @Autowired
    private DbUtils dbUtils;


    public final static int THRESHOLD = 5000;


    /**
     * 统计商品数量之和
     */
    public void sum() {
        //从数据库获取最小ID号
        int recordMin = dbUtils.getMin();
        //从数据库获取最大ID号
        int recordMax = dbUtils.getMax();
        //初始值0
        long sum = 0;

        long start = System.currentTimeMillis();
        int startId = recordMin;

        //每按5000条递增,开始查询数据库5000条
        int endId = recordMin + THRESHOLD;

        while (endId < recordMax) {
            //计算商品数量之和
            sum = sum + dbUtils.sumRecorders(startId, endId);
            startId = startId + THRESHOLD + 1;
            endId = endId + THRESHOLD;
        }
        System.out.println("The sum is " + sum
                + " spend time:" + (System.currentTimeMillis() - start) + "ms");
    }

}
